package cn.iocoder.yudao.module.common.controller.admin.favorites;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.common.controller.admin.favorites.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.favorites.FavoritesDO;
import cn.iocoder.yudao.module.common.service.favorites.FavoritesService;

@Tag(name = "管理后台 - 收藏")
@RestController
@RequestMapping("/common/favorites")
@Validated
public class FavoritesController {

    @Resource
    private FavoritesService favoritesService;

    @PostMapping("/create")
    @Operation(summary = "创建收藏")
    public CommonResult<Long> createFavorites(@Valid @RequestBody FavoritesSaveReqVO createReqVO) {
        return success(favoritesService.createFavorites(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收藏")
    public CommonResult<Boolean> updateFavorites(@Valid @RequestBody FavoritesSaveReqVO updateReqVO) {
        favoritesService.updateFavorites(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收藏")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteFavorites(@RequestParam("id") Long id) {
        favoritesService.deleteFavorites(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收藏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<FavoritesRespVO> getFavorites(@RequestParam("id") Long id) {
        FavoritesDO favorites = favoritesService.getFavorites(id);
        return success(BeanUtils.toBean(favorites, FavoritesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收藏分页")
    public CommonResult<PageResult<FavoritesRespVO>> getFavoritesPage(@Valid FavoritesPageReqVO pageReqVO) {
        PageResult<FavoritesDO> pageResult = favoritesService.getFavoritesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FavoritesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收藏 Excel")
    @OperateLog(type = EXPORT)
    public void exportFavoritesExcel(@Valid FavoritesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FavoritesDO> list = favoritesService.getFavoritesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收藏.xls", "数据", FavoritesRespVO.class,
                        BeanUtils.toBean(list, FavoritesRespVO.class));
    }

}